package com.xhwl.logistics.controller;


import com.xhwl.common.pojo.ResultJson;
import com.xhwl.logistics.bo.cent.VehicleIntegratedConfigBO;
import com.xhwl.logistics.dto.query.DeviceControlDTO;
import com.xhwl.logistics.enums.VehicleParkingLotHandlerEnum;
import com.xhwl.logistics.service.cent.QueryDeviceService;
import com.xhwl.logistics.strategy.IVehicleParkingLotHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

/**
 * 车行设备有设备中台统一管理，故此控制器只提供车行设备的操作接口
 */
//@RestController
public class VehicleDeviceController {


    @Autowired
    private QueryDeviceService queryDeviceService;

    @Autowired
    private ApplicationContext applicationContext;

    /**
     * 开启道闸的接口
     * @return
     */
    @PostMapping("/device/open")
    public ResultJson openDevice(@RequestBody DeviceControlDTO deviceControlDTO){
        VehicleIntegratedConfigBO vehicleIntegratedConfigBO = queryDeviceService.queryIntegratedConfigFromProjectId(deviceControlDTO.getProjectId());
        Integer productId = vehicleIntegratedConfigBO.getVehicleSystemInfoBO().getProductId();

        IVehicleParkingLotHandler vehicleParkingLotHandler = applicationContext.getBean(VehicleParkingLotHandlerEnum.toVehicleParkingLotHandlerEnum(productId).getHandlerName(), IVehicleParkingLotHandler.class);

        vehicleParkingLotHandler.openDevice(vehicleIntegratedConfigBO,deviceControlDTO);

        return ResultJson.successWithVoid();
    }

    /**
     * 关闭道闸的接口
     */
    @PostMapping("/device/close")
    public ResultJson closeDevice(@RequestBody DeviceControlDTO deviceControlDTO){
        VehicleIntegratedConfigBO vehicleIntegratedConfigBO = queryDeviceService.queryIntegratedConfigFromProjectId(deviceControlDTO.getProjectId());
        Integer productId = vehicleIntegratedConfigBO.getVehicleSystemInfoBO().getProductId();

        IVehicleParkingLotHandler vehicleParkingLotHandler = applicationContext.getBean(VehicleParkingLotHandlerEnum.toVehicleParkingLotHandlerEnum(productId).getHandlerName(), IVehicleParkingLotHandler.class);

        vehicleParkingLotHandler.closeDevice(vehicleIntegratedConfigBO,deviceControlDTO);

        return ResultJson.successWithVoid();
    }

    /**
     * 常开道闸的接口
     */
    @PostMapping("/device/all-open")
    public ResultJson allOpenDevice(@RequestBody DeviceControlDTO deviceControlDTO){
        VehicleIntegratedConfigBO vehicleIntegratedConfigBO = queryDeviceService.queryIntegratedConfigFromProjectId(deviceControlDTO.getProjectId());
        Integer productId = vehicleIntegratedConfigBO.getVehicleSystemInfoBO().getProductId();

        IVehicleParkingLotHandler vehicleParkingLotHandler = applicationContext.getBean(VehicleParkingLotHandlerEnum.toVehicleParkingLotHandlerEnum(productId).getHandlerName(), IVehicleParkingLotHandler.class);

        vehicleParkingLotHandler.allOpenDevice(vehicleIntegratedConfigBO,deviceControlDTO);

        return ResultJson.successWithVoid();
    }
}
